import { http } from '@/lib/request'
import type { ResourceItem, FileCourseInfo, ResourceQuery, TagItem } from '../types'

export const commonService = {
  // 提交意见反馈
  feedback: async (contents: string) => {
    return http.post('/feedback/store', {
      contents,
    })
  },

  // 教程列表
  courseList: async (data: {
    pageNum: number
    pageSize: number
    category_sign: 'imageToText' | 'videoCreation'
  }) => {
    return http.get<{ data: FileCourseInfo[]; total: number }>('/course/list', {
      params: data,
    })
  },

  // 资源列表
  // resourceList: async (data: { name: string; exts: '' | 'mp3' | ' png' }) => {
  resourceList: async (params: ResourceQuery) => {
    return http.get<{ data: ResourceItem[]; total: number }>('/resource/list', {
      params,
    })
  },
  translate: async (text: string, target_language: string) => {
    return http.post<string>('/common/translate', {
      target_language,
      text,
    })
  },

  // 删除我的资源
  resourceDelete: async (ids: string) => {
    return http.get<any>(`/resource/delete?ids=${ids}`)
  },
  // 新增/编辑资源
  resourceStore: async (data: {
    id: number | string
    ext: string
    name: string
    url: string
    public: number
    tags: string[]
  }) => {
    return http.post<any>('/resource/store', data)
  },
  // 新增/编辑资源标签
  resourceTagStore: async (data: {
    id: number | string
    ext: string
    name: string
    url: string
    public: string
    tags: string[]
  }) => {
    return http.post<any>('/resourceTag/store', data)
  },

  // 资源标签列表
  getResourceTagList: async () => {
    return http.get<TagItem[]>('resourceTag/list')
  },
  // 删除资源标签
  resourceTagDelete: async (ids: string[]) => {
    return http.get<any>(`/resourceTag/delete?ids=${ids.join(',')}`)
  },

  // 文字合成声音
  getAudioList: async (data: {
    model?: string
    text: string
    voice_setting: {
      voice_id: string
      speed: number
      vol: number
      pitch: number
    }
    audio_setting?: {
      audio_sample_rate: number
      bitrate: number
      format: string
      channel: number
    }
  }) => {
    return http.post('/common/ttsT2AV2', Object.assign({
      model: 'speech-01-turbo',
      audio_setting: {
        audio_sample_rate: 32000, // 音频采萨率
        bitrate: 128000, // 比特率
        format: 'mp3', // 音频格式
        channel: 1, // 音频通道
      }
    }, data))
  },
  // 创建对口型任务
  createLipSyncTask: async (video_url: string, audio_url: string) => {
    return http.post<{ no: string }>('/common/lipSync', {
      video_url,
      audio_url,
    })
  },
  // 获取 对口型任务详情
  getLipSyncTaskDetail: async (noId: string) => {
    return http.get<{
      status: 'COMPLETED' | 'PENDING' | 'FAILED',
      result: {
        outputUrl?: string,
      }
      error_message?: string
    }>(`/common/lipSyncDetail?no=${noId}`, { skipLoading: true })
  },
}
